import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  OneToMany,
} from 'typeorm';
import { ApiProperty, ApiHideProperty } from '@nestjs/swagger';
import { Category } from '../../category/entities/category.entity';
import { Comment } from '../../comment/entities/comment.entity';
import { OrderItem } from '../../order/entities/order-item.entity';

@Entity('books')
export class Book {
  @ApiProperty({ description: '图书ID' })
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty({ description: '书名' })
  @Column()
  title: string;

  @ApiProperty({ description: '作者' })
  @Column()
  author: string;

  @ApiProperty({ description: '出版社' })
  @Column()
  publisher: string;

  @ApiProperty({ description: '出版日期' })
  @Column({ type: 'date' })
  publishDate: Date;

  @ApiProperty({ description: 'ISBN' })
  @Column({ unique: true })
  isbn: string;

  @ApiProperty({ description: '价格' })
  @Column('decimal', { precision: 10, scale: 2 })
  price: number;

  @ApiProperty({ description: '库存数量' })
  @Column('int')
  stock: number;

  @ApiProperty({ description: '图书简介' })
  @Column('text', { nullable: true })
  description: string;

  @ApiProperty({ description: '创建时间' })
  @CreateDateColumn()
  createdAt: Date;

  @ApiProperty({ description: '更新时间' })
  @UpdateDateColumn()
  updatedAt: Date;

  @ApiProperty({ description: '所属分类', type: () => Category })
  @ManyToOne(() => Category, (category) => category.books, { nullable: true })
  @JoinColumn({ name: 'category_id' })
  category: Category;

  @ApiProperty({ description: '分类ID' })
  @Column({ nullable: true })
  category_id: string;

  @ApiHideProperty()
  @OneToMany(() => Comment, comment => comment.book)
  comments: Comment[];

  @ApiHideProperty()
  @OneToMany(() => OrderItem, orderItem => orderItem.book)
  orderItems: OrderItem[];
}